package com.lnsf.controller;

import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.lnsf.entity.Goods;
import com.lnsf.entity.User;
import com.lnsf.service.IGoodsService;
import com.lnsf.service.IUserService;
import com.lnsf.util.CodeUtil;

/*
 * UserController层
 * author:黄华炎
 * 2017-07-26
 * 
 * 
 */

@Controller
@RequestMapping("/user")
@SessionAttributes("currentUser")
public class UserController {

	@Autowired
	private IUserService iUserService;
	
	@Autowired
    private IGoodsService iGoodsService;
	
	 private static final Logger log = Logger.getLogger(UserController.class);// 日志文件
	 
	//注册，根据检测手机号是否注册过
	 @RequestMapping("register")
	 public ModelAndView regist(User user,HttpSession session) {  
	        ModelAndView mav=new ModelAndView();  
	       
	        try {  
	        	//转义邮件的@字符
	        	String email=URLDecoder.decode(user.getEmail(),"utf-8");  
	        	user.setEmail(email);
	        	
	        	//获取一个邮箱验证码
	        	String code = CodeUtil.generateUniqueCode();
	        	user.setCode(code);
	        	user.setActive(0);  //设置未激活
	        	user.setPower(1);   //1 普通用户权限
	            iUserService.register(user);  
	        //  request.setAttribute("user", user);  
	            mav.setViewName("login");  
	            mav.addObject("user", user);  
	            mav.addObject("msg", "注册成功了，可以去登陆了");  
	            return mav;  
	        } catch (Exception e) {  
	            mav.setViewName("menu");  
	            mav.addObject("user", null);  
	            mav.addObject("msg", "注册失败");  
	            return mav;  
	        }  
	         
	 }
	
	
	
	//邮箱验证成功
	@RequestMapping("/commitRegister")
	public String commitRegister(String code) {  
		
		int active = iUserService.activeUser(code);
		if(active>0){
			return "login";
		}
		else{
			return "fail";
		}
		
		
	}
	
	//登陆,根据手机号，密码登陆
	@RequestMapping("/login")
	public String login(User user,HttpServletRequest request){
		User resultUser = iUserService.login(user);
		log.info("request: user/login , user: " + user.toString());
		System.out.println("xxx"+resultUser);
        if (resultUser.getUsername().length()<0&&resultUser.getPassword().length()<0 ) {
            request.setAttribute("user", user);
            request.setAttribute("errorMsg", "请认真核对账号、密码！");
            return "login";
        } else if(resultUser != null && resultUser.getActive()== 0){
        	request.setAttribute("user", user);
        	request.setAttribute("errorMsg", "请通过邮箱认证账号");
        	return "login";
        }
        else{
        	 HttpSession session = request.getSession();
             session.setAttribute("currentUser", resultUser);
             return "main";
        }
		
	}
	//分页查询用户
	@RequestMapping("/list")
	@ResponseBody
	public Iterable<User> getUserlist(@RequestParam(name="pageNum",required=false)Integer pageNum ,
						@RequestParam(name="pageSize",required=false)Integer pageSize){
		System.out.println(pageNum+"--"+pageSize);
		Iterable<User> iterable = iUserService.getUserByPage(pageNum, pageSize);
		System.out.println(iterable);
		return iterable;
	}
	
	//修改用户
	@RequestMapping("/modify")
	@ResponseBody
	public String modifyUser(User user){
		
		
		
		return null;
	}
	
	//模糊查询
	@RequestMapping("/findByCondition")
	@ResponseBody
	public List<User> findByCondition(User user){
		System.out.println("User:"+user);
		List<User>  list= iUserService.getUserByCondition(user);
		return list;
	}
	
	//退出登录
	@RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
		session.invalidate();
		log.info("request: user/logout");
        return "redirect:login.jsp";
    }
	
	//获得个人的闲置商品
	@RequestMapping(value = "/getGoods")
	@ResponseBody
    public List<Goods> getGoods(HttpSession session,User user) {
		//通过session中的当前用户去获得用户信息
		//User currentUser = (User) session.getAttribute("currentUser");
		
		System.out.println("user:"+user);
		List<Goods> goods = iGoodsService.findGoodsByUser(user);
        return goods;
    }
	
	
	
	
	
}
